package com.hdkj.serviceai.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hdkj.commonutils.R;
import com.hdkj.serviceai.conutils.ConUtil ;
import com.hdkj.serviceai.service.KuaidiService;
import com.hdkj.serviceai.service.LossfoundService;
import com.hdkj.serviceai.service.TrainService;
import com.hdkj.serviceai.utils.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/eduai/dishonesty")
public class MoreApiController {

    @Autowired
    private LossfoundService lossfoundService;

    @Autowired
    private TrainService trainService;

    @Autowired
    private KuaidiService kuaidiService;

    /**
     * 失信人执行名单
     * @param map
     * @return
     * @throws IOException
     */
    @PostMapping("lossfound")
    public R lossfound(@RequestBody Map<String, Object> map) throws IOException {
      Object result= lossfoundService.getlossinfo(map);
      return R.ok().data("result", result);
    }


    @PostMapping("totrain")
    public R totrain(@RequestBody Map<String, Object> map) throws IOException {
       Object result=trainService.gettraininfo(map);
       return R.ok().data("result", result);
    }


    @PostMapping("kuaidi")
    public R kuaidi(@RequestBody Map<String, Object> map) throws Exception {
        Object result = kuaidiService.getkuaidiinfo(map);
        return R.ok().data("result", result);
    }


    @PostMapping("weather")
    public R weather(@RequestBody Map<String, Object> map) throws Exception {
        String host = "https://jisutqybmf.market.alicloudapi.com";
        String path = "/weather/query";
        String method = "get";//GET/POST 任意
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + ConUtil.getkey());
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/json; charset=UTF-8");
        Map<String, Object> querys = new HashMap<String, Object>();
        querys.put("city", "芜湖");
        querys.put("citycode", "citycode");
        querys.put("cityid", "cityid");
        querys.put("ip", "ip");
        querys.put("location", "location");
        try {
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            //获取response的body
            System.out.println(EntityUtils.toString(response.getEntity(), "utf-8"));

            JSONObject jsonObject = JSON.parseObject(EntityUtils.toString(response.getEntity()));

            System.out.println(jsonObject);

            return R.ok().data("result", jsonObject.get("result"));


        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}